package com.lottery.im.module.service.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.ServiceData;
import java.util.List;

public class ServiceGridAdapter extends BaseAdapter {

  private Context mContext;
  private LayoutInflater mInflater;//布局装载器对象
  private List<ServiceData.ReturnListBean> listBeans;
  private OnServiceItemClickListener mOnServiceItemClickListener;

  public ServiceGridAdapter(Context context, List<ServiceData.ReturnListBean> listBeans) {
    this.listBeans = listBeans;
    mInflater = LayoutInflater.from(context);
    mContext = context;
  }

  @Override public int getCount() {
    return listBeans.size();
  }

  @Override public Object getItem(int position) {
    return listBeans.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder ;
    if (null == convertView) {
      convertView = mInflater.inflate(R.layout.item_service_grid, null);
      holder = new ViewHolder();
      holder.tvTitle = convertView.findViewById(R.id.tv_title);
      holder.ivIcon = convertView.findViewById(R.id.iv_icon);
      holder.layout = convertView.findViewById(R.id.layout);
      holder.lineR = convertView.findViewById(R.id.line_r);
      holder.lineB = convertView.findViewById(R.id.line_b);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    ServiceData.ReturnListBean bean = listBeans.get(position);
    holder.tvTitle.setText(bean.getName());
    if (TextUtils.isEmpty(bean.getImg_url()) && position == 0) {
      holder.ivIcon.setImageResource(R.drawable.icon_signin);
    } else if (TextUtils.isEmpty(bean.getImg_url()) && position == 1) {
      holder.ivIcon.setImageResource(R.drawable.icon_fw_zdhb);
    } else {
      Glide.with(mContext)
          .load(Api.BASEADDRESS + bean.getImg_url())
          .apply(new RequestOptions().placeholder(R.drawable.icon_wait)
              .error(R.drawable.icon_wait).fitCenter())
          .into(holder.ivIcon);
    }

    if (position % 3 == 0 || position % 3 == 1) {
      holder.lineR.setVisibility(View.VISIBLE);
    } else {
      holder.lineR.setVisibility(View.GONE);
    }

    if (position / 3 == listBeans.size() / 3) {
      holder.lineB.setVisibility(View.GONE);
    } else {
      holder.lineB.setVisibility(View.VISIBLE);
    }

    holder.layout.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (null != mOnServiceItemClickListener)mOnServiceItemClickListener.OnServiceItemClick(position);
      }
    });

    return convertView;
  }

  class ViewHolder {
    TextView tvTitle;
    ImageView ivIcon;
    LinearLayout layout;
    View lineR;
    View lineB;
  }

  public void setOnServiceItemClickListener(
      OnServiceItemClickListener onServiceItemClickListener) {
    mOnServiceItemClickListener = onServiceItemClickListener;
  }

  public interface OnServiceItemClickListener {
    void OnServiceItemClick(int position);
  }

}
